package com.ys.everydaynews.ui.main.activityy

import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.ys.everydaynews.R
import com.ys.everydaynews.base.BaseActivity
import com.ys.everydaynews.base.Constants.MOBLIE
import com.ys.everydaynews.databinding.LayoutRegisterBinding
import com.ys.everydaynews.ui.main.model.EventModel
import com.ys.everydaynews.ui.main.presenter.RegisterPresenter
import com.ys.everydaynews.ui.main.view.RegisterView
import com.ys.everydaynews.utils.CommonUtils.checkPhoneNum
import com.ys.everydaynews.utils.CommonUtils.replaceBlank
import com.ys.everydaynews.view.MyCheckBox

class RegisterAtivity : BaseActivity<RegisterView, RegisterPresenter, LayoutRegisterBinding>(),
    TextWatcher {

    private var phoneNumber: String = ""

    override fun getLayoutId(): Int = R.layout.layout_register
    override fun isRegister(): Boolean =false
    override fun initView() {
        binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        binding.btnBack.setOnClickListener { finish() }
        binding.edtMoblie.addTextChangedListener(this)
        binding.checkbox.initSpan(R.string.xy)
        binding.checkbox.isShow(true)
        binding.checkbox.setOnClickListener {
            binding.checkbox.changeState()
            checkPhone()
        }
        binding.checkbox.setClickableSpanText(object : MyCheckBox.ClickableSpanText {
            override fun onClick(type: Int) {
                if (type == 1) {
                    Toast.makeText(this@RegisterAtivity, "用户协议", Toast.LENGTH_SHORT).show()
                } else {
                    Toast.makeText(this@RegisterAtivity, "隐私政策", Toast.LENGTH_SHORT).show()
                }
            }
        })
        binding.cdNext.setOnClickListener {
            if (TextUtils.isEmpty(phoneNumber)) {
                Toast.makeText(this@RegisterAtivity, "请填写正确电话", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            if (!binding.checkbox.isChecked()) {
                Toast.makeText(this@RegisterAtivity, "请先勾选协议", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
            //TODO 验证码跳转
            val intent=Intent(this,SendCodeActivity::class.java)
            intent.putExtra(MOBLIE,phoneNumber)
            startActivity(intent)
        }
    }

    override fun initData() {
    }

    override fun createPersenter(): RegisterPresenter = RegisterPresenter()

    override fun <T> setData(data: T) {
    }

    override fun setError(err: String) {
    }

    private fun checkPhone() {
        if (phoneNumber.length == 13 && binding.checkbox.isChecked() && checkPhoneNum(replaceBlank(phoneNumber))) {
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_red))
        } else {
            binding.cdNext.setCardBackgroundColor(ContextCompat.getColor(this, R.color.jx_gray))
        }
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        phoneNumber = s.toString()
        checkPhone()
    }

    override fun afterTextChanged(s: Editable?) {
    }

    override fun getMessage(message: EventModel<*>) {
        super.getMessage(message)
        if (message!=null){
            if (message.code==101){
                finish()
            }
        }
    }
}